/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
/*
 * JAVA-programmering, med fokus på reMIND-utveckling
 * File:          Register.java
 * Classes:       Register, CJFrame
 *
 *
 * Date 2009-05-21
 *
 * Revision history:
 * Version 1.0 2002-01-29 First version
 * Version 1.1 2002-01-30 Corrected unwanted behaviour in the GUI
 *
 */
import java.util.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;

/**
 * Undocumented
 * @author  Jonas Boustedt
 * @version 1.1 2002-01-30
 */
/**
 *
 * @author nawma77
 * @version 1.2 2009-05-21
 */
public class Register extends JFrame{

    private JLabel ssnLabel           = new JLabel("Social Security Number: ");
  private JTextField ssnText        = new JTextField(11);
  private JLabel firstNameLabel     = new JLabel("First Name: ");
  private JTextField firstNameText  = new JTextField(30);
  private JLabel lastNameLabel      = new JLabel("Last Name: ");
  private JTextField lastNameText   = new JTextField(30);
  private JLabel addressLabel       = new JLabel("Address: ");
  private JTextField addressText    = new JTextField(30);
  private JLabel phoneLabel         = new JLabel("Phone: ");
  private JTextField phoneText      = new JTextField(30);
  private JRadioButton femaleButton = new JRadioButton("Female");
  private JRadioButton maleButton   = new JRadioButton("Male");
  private JRadioButton neutralButton= new JRadioButton("No Gender");
  private ButtonGroup buttonGroup   = new ButtonGroup();
  private JList personList          = new JList();
  private HashMap personMap         = new HashMap();
  private JButton newButt           = new JButton("New");
  private JButton insertButt        = new JButton("Insert to list");
  private JButton updateButt        = new JButton("Update");
  private JButton removeButt        = new JButton("Remove from list");
  private JButton quitButt          = new JButton("Quit");
  private JMenuItem miQuit          = new JMenuItem("Quit");
  private JMenuItem miHow           = new JMenuItem("How to");
  private JMenuItem miAbout         = new JMenuItem("About");

  /**
   * Undocumented
   * @param
   * @return
   */
  public Register()
    {
    super("Person Registrator");
    JMenu mFile = new JMenu("File");
    mFile.setToolTipText("File actions");
    JMenu mHelp = new JMenu("Help");
    mFile.add(miQuit);
    femaleButton.setEnabled(false);
    maleButton.setEnabled(false);
    neutralButton.setEnabled(false);
    buttonGroup.add(femaleButton);
    buttonGroup.add(maleButton);
    buttonGroup.add(neutralButton);
    mHelp.add(miHow);
    mHelp.add(miAbout);
    JMenuBar mbar = new JMenuBar();
    mbar.add(mFile);
    mbar.add(mHelp);
    insertButt.setToolTipText("Insert this person to registry");
    updateButt.setToolTipText("Update this person in registry");
    removeButt.setToolTipText("Remove person from registry");
    quitButt.setToolTipText("Quit this program");
    JPanel lpanel = new JPanel();
    JPanel lWpanel = new JPanel();
    JPanel lEpanel = new JPanel();
    JPanel rpanel = new JPanel();
    JPanel rWpanel = new JPanel();
    JPanel rEpanel = new JPanel();
    JPanel bpanel = new JPanel();
    JPanel mpanel = new JPanel();
    JPanel npanel = new JPanel();
    JPanel nEpanel = new JPanel();
    JPanel nWpanel = new JPanel();

    npanel.setLayout(new java.awt.BorderLayout());
    npanel.add(nWpanel, java.awt.BorderLayout.WEST);
    npanel.add(nEpanel, java.awt.BorderLayout.EAST);
    nWpanel.setLayout(new java.awt.GridLayout(0,1));
    nWpanel.add(ssnLabel);
    nWpanel.add(firstNameLabel);
    nWpanel.add(lastNameLabel);
    nWpanel.add(addressLabel);
    nWpanel.add(phoneLabel);
    nEpanel.setLayout(new java.awt.GridLayout(0,1));
    nEpanel.add(ssnText);
    nEpanel.add(firstNameText);
    nEpanel.add(lastNameText);
    nEpanel.add(addressText);
    nEpanel.add(phoneText);

    lpanel.setLayout(new java.awt.BorderLayout());
    lpanel.add(lWpanel,java.awt.BorderLayout.WEST);
    lpanel.add(lEpanel,java.awt.BorderLayout.EAST);

    lWpanel.setLayout(new java.awt.GridLayout(0,1));
    lWpanel.add(neutralButton);
    lWpanel.add(femaleButton);
    lWpanel.add(maleButton);
    lEpanel.setLayout(new java.awt.GridLayout(0,1));
    lEpanel.add(new JLabel());
    lEpanel.add(new JLabel());

    rpanel.setLayout(new java.awt.BorderLayout());
    rpanel.add(rWpanel,java.awt.BorderLayout.WEST);
    rpanel.add(rEpanel,java.awt.BorderLayout.EAST);

    rWpanel.setLayout(new java.awt.GridLayout(0,1));

    rWpanel.add(new JLabel());
    rEpanel.setLayout(new java.awt.GridLayout(0,1));
    rEpanel.add(new JLabel());

    bpanel.setLayout(new java.awt.FlowLayout());
    bpanel.add(newButt);
    bpanel.add(insertButt);
    bpanel.add(updateButt);
    bpanel.add(removeButt);
    bpanel.add(quitButt);

    mpanel.setLayout(new BorderLayout());
    JPanel upanel = new JPanel();
    upanel.setLayout(new BorderLayout());
    upanel.add(npanel,BorderLayout.NORTH);
    upanel.add(lpanel,BorderLayout.WEST);
    upanel.add(rpanel,BorderLayout.EAST);
    mpanel.add(upanel,BorderLayout.NORTH);
    mpanel.add(new JScrollPane(personList),BorderLayout.CENTER);
    mpanel.add(bpanel,BorderLayout.SOUTH);
    setJMenuBar(mbar);
    getContentPane().add(mpanel,BorderLayout.CENTER);
    addWindowListener(new WindowAdapter(){public void windowClosing(WindowEvent e){quit();}});
    miQuit.addActionListener(new ActionListener(){public void actionPerformed(ActionEvent e){quit();}});
    miAbout.addActionListener(new ActionListener(){public void actionPerformed(ActionEvent e){showAbout();}});
    miHow.addActionListener(new ActionListener(){public void actionPerformed(ActionEvent e){showHelp();}});
    newButt.addActionListener(new ActionListener(){public void actionPerformed(ActionEvent e){clearForm();}});
    insertButt.addActionListener(new ActionListener(){public void actionPerformed(ActionEvent e){insertPerson();}});
    updateButt.addActionListener(new ActionListener(){public void actionPerformed(ActionEvent e){updatePerson();}});
    removeButt.addActionListener(new ActionListener(){public void actionPerformed(ActionEvent e){removePerson();}});
    quitButt.addActionListener(new ActionListener(){public void actionPerformed(ActionEvent e){quit();}});
    personList.addListSelectionListener(new ListSelectionListener(){public void valueChanged(ListSelectionEvent e){listSelection();}});
    ssnText.addFocusListener(new FocusAdapter(){public void focusGained(FocusEvent e){clearSelection();}});
    firstNameText.addFocusListener(new FocusAdapter(){public void focusGained(FocusEvent e){clearSelection();}});
    lastNameText.addFocusListener(new FocusAdapter(){public void focusGained(FocusEvent e){clearSelection();}});
    addressText.addFocusListener(new FocusAdapter(){public void focusGained(FocusEvent e){clearSelection();}});
    phoneText.addFocusListener(new FocusAdapter(){public void focusGained(FocusEvent e){clearSelection();}});
    clearForm();
    setSize(500,400);
    centerOnScreen();
    setVisible(true);
    }

  /**
   * Undocumented
   * @param
   * @return
   */
  private boolean checkForm()
    {
    boolean result=true;
    if(ssnText.getText().length()<1)
      result=false;
    if(firstNameText.getText().length()<1)
      result=false;
    if(lastNameText.getText().length()<1)
      result=false;
    return result;
    }

  /**
   * Undocumented
   * @param
   * @return
   */
  private void clearSelection()
    {
    personList.clearSelection();
    neutralButton.setSelected(true);
    }

  /**
   * Undocumented
   * @param
   * @return
   */
  private void clearForm()
    {
    ssnText.setText("");
    firstNameText.setText("");
    lastNameText.setText("");
    addressText.setText("");
    phoneText.setText("");
    ssnText.requestFocus();
    clearSelection();
    }

  /**
   * Undocumented
   * @param
   * @return
   */
  private void displayPerson(Person person)
    {
    if(person.isOK())
      {
      ssnText.setText(person.getSSN());
      firstNameText.setText(person.getFirstName());
      lastNameText.setText(person.getLastName());
      addressText.setText(person.getAddress());
      phoneText.setText(person.getPhone());
      if(person.isFemale())
        femaleButton.setSelected(true);
      else
        maleButton.setSelected(true);
      }
    }

  /**
   * Undocumented
   * @param
   * @return
   */
  private Person makePerson()
    {
    Person person = new Person("");     // a 'bad' person
    if(checkForm())
      {
      person = new Person(ssnText.getText());  // a new, hopfully 'ok' person
      if(person.isOK())
        {
        person.setFirstName(firstNameText.getText());
        person.setLastName(lastNameText.getText());
        person.setAddress(addressText.getText());
        person.setPhone(phoneText.getText());
        }
      else
        JOptionPane.showMessageDialog(this,"This person has a bad SSN");
      }
    else
      JOptionPane.showMessageDialog(this,"Missing some mandatory fields (ssn, first name, last name)");
    return person;
    }

  /**
   * Undocumented
   * @param
   * @return
   */
  private void insertPerson()
    {
    Person person = makePerson();
    if(person.isOK())
      {
      if(personMap.get(person.getSSN())==null)
        {
        personMap.put(person.getSSN(), person);
        updateList();
        clearForm();
        }
      else
        JOptionPane.showMessageDialog(this,"This person does already exist (use update)");
      }
    }

  /**
   * Undocumented
   * @param
   * @return
   */
  private void updatePerson()
    {
    Person person = makePerson();
    if(person.isOK())
      {
      if(personMap.get(person.getSSN())!=null)
        {
        personMap.put(person.getSSN(), person);
        updateList();
        clearForm();
        }
      else
        JOptionPane.showMessageDialog(this,"This person does not exist (use insert)");
      }
    }

  /**
   * Undocumented
   * @param
   * @return
   */
  private void removePerson()
    {
    Person person = makePerson();
    if(person.isOK())
      {
      if(personMap.get(person.getSSN())!=null)
        {
        personMap.remove(person.getSSN());
        updateList();
        clearForm();
        }
      else
        JOptionPane.showMessageDialog(this,"This person does not exist");
      }
    }

  /**
   * Undocumented
   * @param
   * @return
   */
  private void listSelection()
    {
    Person person = (Person)personList.getSelectedValue();
    if(person!=null && person.isOK())
      displayPerson(person);
    }

  /**
   * Undocumented
   * @param
   * @return
   */
  private void updateList()
    {
    personList.setListData(new Vector(personMap.values())); // HashMap -> Collection -> Vector -> JList
    }

  /**
   * Undocumented
   * @param
   * @return
   */
  private void quit()
    {
    if(JOptionPane.showConfirmDialog(this, "Do You really want to quit?")==JOptionPane.YES_OPTION)
      System.exit(0);
    }

  private void showAbout()
    {
    JTextArea textArea = new JTextArea();
    textArea.append("Person Registrator 1.0.0\n");
    textArea.append("This software was developed by\nJonas Boustedt (teacher)\nand students\n");
    textArea.append("Free to use by staff and students\n");
    textArea.append("at the N-institution of HiG\n");
    textArea.append("(C) 2002\nAll rights reserved\n");
    textArea.setEditable(false);
    JOptionPane.showMessageDialog(this,textArea,"About Person Registrator",JOptionPane.INFORMATION_MESSAGE);
    }

  private void showHelp()
    {
    JTextArea textArea = new JTextArea();
    textArea.append("KURS\n");
    textArea.append("TEMS - JAVA-programmering - från grundläggande programmering till utveckling av modelleringsverktyget reMIND\n\n");
    textArea.append("ALLMÄNT\n");
    textArea.append("Detta program används i Laboration 1 för att testa klassen Person.\n");
    textArea.append("\n");
    textArea.setEditable(false);
    JOptionPane.showMessageDialog(this,new JScrollPane(textArea),"How to...",JOptionPane.INFORMATION_MESSAGE);
    }

  /**
   * Undocumented
   * @param
   * @return
   */
  private void fixMenuBug()
    {
    setSize(getWidth()+1, getHeight());
    setSize(getWidth()-1, getHeight());
    }

 

  public void centerOnScreen()
    {
    Dimension d = getToolkit().getScreenSize();
    Rectangle b = getBounds();
    setLocation((d.width-b.width)/2, (d.height-b.height)/2);
    }
}

